import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class GlobalToolService {

  constructor(
    private http: HttpClient
  ) { }

  async inject(context?:any) {
    const c = context || window;

    if (c) {
      c.iasGlobalTools = {
        chatAI: this.chatAI.bind(this),
        pushNotification: this.pushNotification.bind(this),
        userHistories: this.userHistories.bind(this),
      };
    }
  }

  /**
   * @description: This function is used to call the chatAI api
   * @returns: void
   */
  async chatAI(message:string) {
    if(!message) {
      return;
    }

    return this.http.post('/api/chat',{data:{message}}).subscribe((res:Record<string,any>) => {
      return res;
    })
  }

  /**
   * @description: This function is used to call the push notification api
   * @param notificationOptions: chrome.notifications.NotificationOptions<true>
   * @returns: void
   */
  async pushNotification(notificationOptions:chrome.notifications.NotificationOptions<true>) {
    chrome.notifications.create(new Date().getTime().toString(),{
      title: notificationOptions.title,
      type: 'basic',
      message: notificationOptions.message,
      iconUrl: chrome.runtime.getURL('assets/icons/error-icon.jpg')
    })
  }

  /**
   * @description: This function is used to call the user history api
   * @returns: void
   */
  async userHistories() {
    return this.http.get('/api/user-histories').subscribe((res:Record<string,any>) => {
      return res;
    })
  }
}
